<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-cpu"></i> 设备列表 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-input v-model="eqSn" placeholder="请搜索设备编号" class="handle-input mr10" @keyup.enter.native="handleSearch" ></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                <el-button type="success" icon="el-icon-plus" @click="AddEq">新增设备</el-button>
            </div>
            <el-table :data="tableData" border class="table" ref="multipleTable" header-cell-class-name="table-header">
                <el-table-column prop="f_equip_id" label="设备ID" align="center"></el-table-column>
                <el-table-column prop="f_union_code" label="设备编号"></el-table-column>
                <el-table-column prop="f_equip_name" label="设备名称"></el-table-column>
                <el-table-column prop="f_status" label="设备状态">
                    <template slot-scope="scope">
                        <span v-if="scope.row.f_status == 1">在线</span>
                        <span v-if="scope.row.f_status == 0">离线</span>
                    </template>
                </el-table-column>
                <el-table-column prop="f_equip_uuid" label="设备平台标识"></el-table-column>
                <el-table-column prop="has_shop.f_shop_name" label="店铺名称"></el-table-column>
                <el-table-column prop="has_shop.f_shopowner" label="店主姓名"></el-table-column>
                <el-table-column prop="has_shop.f_phone" label="联系方式"></el-table-column>
                <el-table-column label="所在省市区">
                    <template slot-scope="scope"
                        >{{ scope.row.has_shop.f_provinces }}{{ scope.row.has_shop.f_city }}{{ scope.row.has_shop.f_area }}</template
                    >
                </el-table-column>
                <el-table-column prop="has_shop.f_address" label="详细地址"></el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button type="text" icon="el-icon-connection" @click="check(scope.row)">设备关联商家</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.page"
                    :page-size="query.pageNum"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>
        <!-- 设备关联商家 -->
        <el-dialog title="设备关联商家" :visible.sync="shopshow" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="商家店铺">
                    <el-select v-model="shopvalue" placeholder="请选择关联商家店铺">
                        <el-option v-for="item in shopList" :key="item.value" :label="item.label" :value="item.value"> </el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="shopsaveEdit()">确 定</el-button>
            </span>
        </el-dialog>
        <!-- 新增设弹出框 -->
        <el-dialog title="新增设备" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="设备编号">
                    <el-input placeholder="请输入设备编号" v-model="eqid"></el-input>
                </el-form-item>
                <el-form-item label="商家店铺">
                    <el-select v-model="value" placeholder="请选择商家店铺">
                        <el-option v-for="item in shopList" :key="item.value" :label="item.label" :value="item.value"> </el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit()">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { apply, createEquipmeng, LinkShop, shopList, getAllEqList, opWithDrawal } from '../../../api/index';
export default {
    name: 'basetable',
    data() {
        return {
            query: {
                page: 1,
                pageNum: 15
            },
            tableData: [],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            shopshow: false,
            pageTotal: 0,
            form: {},
            shopList: [
                {
                    label: '',
                    value: ''
                }
            ],
            label: '',
            value: '',
            shopvalue: '',
            eqid: '',
            shopeqid:'',
            eqSn:'',
        };
    },
    created() {
        this.getData();
    },
    methods: {
        // 列表展示数据
        getData() {
            getAllEqList(this.query).then((res) => {
                console.log(res.data.list, '设备列表');
                this.tableData = res.data.list;
                this.pageTotal = res.data.count || 50;
            });
        },
          // 触发搜索按钮
          handleSearch() {
            if (this.eqSn == '') {
                this.getData();
            }
            console.log(this.orderSn, '商家姓名');
            getAllEqList({
                page: 1,
                pageNum: 15,
                eqSn:this.eqSn
            }).then((res) => {
                console.log(res.data.list, '设备列表');
                this.tableData = res.data.list;
                this.pageTotal = res.data.count || 50;
            });
        },
        // 店铺列表数据
        shop() {
            shopList({
                type: 2 //商家列表
            }).then((res) => {
                console.log(res.data, '循环出来的商家列表');
                let arr = res.data;
                this.shopList = arr.map((item) => {
                    return {
                        label: item.f_shop_name,
                        value: item.id
                    };
                });
                console.log(this.shopList, '循环');
            });
        },
        //新增设备
        AddEq() {
            this.editVisible = true;
            this.shop();
        },
        // 新增设备保存
        saveEdit() {
            if (this.eqid == '') {
                this.$message.warning(`请输入设备号`);
                return;
            }
            if (this.value == '') {
                this.$message.warning(`请选择商家店铺`);
                return;
            }
            this.editVisible = false;
            createEquipmeng({
                unid: this.eqid, //设备id
                shopId: this.value //商家id
            }).then((res) => {
                if(res.code == 0){
                    this.$message.success(`新增设备添加成功`);
                }
                console.log(res, '新增设备返回的数据');
                this.getData()
            });
        },
        //设备关联商家按钮
        check(e) {
            this.shopeqid = e.id
            console.log(this.shopeqid,'shopeqid');
            this.shopshow = true;
            this.shop();
        },
        // 更改设备保存
        shopsaveEdit() {
            if (this.shopvalue == '') {
                this.$message.warning(`请选择商家店铺`);
                return;
            }
            this.shopshow = false;
            //更改设备接口
            LinkShop({
                eqId:  this.shopeqid, //设备id
                shopId: this.shopvalue //商家id
            }).then((res) => {
                if(res.code == 0){
                    this.$message.success(`此设备更改商家成功`);
                }
                console.log(res, '更改设备返回的数据');
            });
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'page', val);
            this.getData();
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.green {
    color: rgb(38, 141, 38);
}
.red {
    color: red;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
